<template>
    <div style="display:block;">
        <el-dialog title="选择体检模板" :visible.sync="show" @close="closeWin">
            <div class="top-wrap">
                <div class="title">体检模板</div>
                <el-tag  v-for="item,index in tijian_list" :key="item.muban_name" closable style="margin-left:6px;" @close="deleteJob(item.id)">
          <span  @click.stop="chooseMuban(index)" style="cursor:pointer">
            {{item.muban_name}}
          </span>
                </el-tag>
            </div>
            <el-form :rules="formRules" ref="dataNoticeForm" :model="bean" label-position="right" label-width="80px"
                     style='width: 600px; margin:20px auto;'>
                <el-form-item label="医院名称" prop="hospital_name">
                    <el-input v-model="bean.hospital_name" placeholder="输入医院名称"></el-input>
                </el-form-item>
                <el-form-item label="医院地址" prop="hospital_address">
                    <el-input v-model="bean.hospital_address" placeholder="输入医院地址"></el-input>
                </el-form-item>
                <el-form-item label="体检时间" prop="tijian_time">
                    <el-date-picker style="width:520px" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                                    v-model="bean.tijian_time"
                                    type="datetime"
                                    placeholder="选择体检时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="乘车路线" prop="tijian_route">
                    <el-input v-model="bean.tijian_route" placeholder="输入乘车路线" type="textarea"></el-input>
                </el-form-item>
                <el-form-item label="备注信息" prop="tijian_remark">
                    <el-input v-model="bean.tijian_remark" placeholder="输入备注信息"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="doSaveMuban">另存为模板</el-button>
                <el-button type="primary" @click="doSubmit">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
	import API from '@/api'
	import Http from '@/http'
	import Msg from '@/utils/msg'
	import _ from 'underscore'
	import Store from '@/store'
	
	export default {
		name: "cityPicker",
		data() {
			return {
				bean: {},
				company:Store.getters.user.company,
				formRules: {
					hospital_name: [{required: true, message: '请填写医院名称', trigger: 'blur'}],
					hospital_address: [{required: true, message: '请填写医院地址', trigger: 'blur'}],
					tijian_time: [{required: true, message: '请选择体检时间', trigger: 'blur'}],
					tijian_route: [{required: true, message: '请填写乘车路线', trigger: 'blur'}],
				},
				tijian_list:[],
				notice_id:'',
				show: true
			}
		},
		props: {
			ids: {
				type: String,
				default() {
					return ""
				}
			},
			mobiles: {
				type: String,
				default() {
					return ""
				}
			},
		},
		computed: {},
		created() {
			this.loadNotice()
		},
		methods: {
			closeWin() {
				this.$emit('tijianWinClose')
			},
			chooseMuban(index){
				this.bean = this.tijian_list[index]
			},
			loadNotice(){
				Http.getModel(API.get_notice_cache, {tpl_type: 3}, false).then((json) => {
					this.dealing = false
					if (json.code == 0) {
						this.tijian_list = JSON.parse(json.data.content)
						this.notice_id = json.data.id
					} else {
						Msg.error(json.msg)
					}
				})
			},
			doSaveMuban(){
				this.$refs['dataNoticeForm'].validate((valid) => {
					if (valid) {
						Msg.prompt("请补充模板名",(val)=>{
							var current_bean = {
								hospital_name:this.bean.hospital_name,
								hospital_address:this.bean.hospital_address,
								tijian_time:this.bean.tijian_time,
								tijian_route:this.bean.tijian_route,
								tijian_remark:this.bean.tijian_remark,
								muban_name:val
							}
							var tijian_list = _.clone(this.tijian_list);
							tijian_list.unshift(current_bean)
							var params = {
								id:this.notice_id,
								tpl_type: 3,
								content:JSON.stringify(tijian_list)
							}
							Http.getModel(API.save_notice_cache, params, false).then((json) => {
								this.dealing = false
								if (json.code == 0) {
									Msg.success("保存成功！",()=>{
										this.loadNotice()
									})
								} else {
									Msg.error(json.msg)
								}
							})
						})
					}
				})
			},
			doSubmit() {
				console.log("mobiles:",this.mobiles)
				this.$refs['dataNoticeForm'].validate((valid) => {
					if (valid) {
						var params = this.bean
						var content = "短信正文： "+ '您好,请准备参加公司体检，体检时间：' + params.tijian_time + ',体检医院：' + params.hospital_name + ',医院地址：'
							+ params.hospital_address + ',乘车路线：' + params.tijian_route
							+ (params.tijian_remark ? ("(" + params.tijian_remark + ")。") : "。") + "(" + this.company.name + ")";
						Msg.alert(content,()=>{
							Http.getModel(API.send_notice, {content:content,mobiles:this.mobiles}, false).then((json) => {
								this.dealing = false
								if (json.code == 0) {
									Msg.success("发送成功！",()=>{
										
										Http.getModel(API.set_mianshi_notice_flag, {ids: this.ids}, false).then((json) => {
											if (json.code == 0) {
												this.$emit('refreshList')
												this.closeWin()
											}
										})
									})
								} else {
									Msg.error(json.msg)
								}
							})
						})
					}
				})
			}
		}
	};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
    .top-wrap{
        padding:10px 50px;
        .title{
            margin-bottom:10px;
            font-weight:bold;
            color:#666;
            font-size:16px;
        }
    }
</style>

